<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
    Class: Auth

    Author: 
        Lonnie Ezell
*/
class JimMenu{
    private $ci;
    
    public function __construct($config = array())
    {
        $this->ci =& get_instance();
        //log_message('debug', "Email Class Initialized");
        if (!class_exists('menu_model')){
            $this->ci->load->model('menu_model');
        }
        //$user_login = $this->ci->user_model->getUserByName($username);
    }
    
    function menuParent($menuId = 0,$status = null)
    {//load menu da cap, $menuId = 0 => lay tat ca menu, != lay tat ca menu con cua menu do 
        $menu_parent = array();
        $menu = array();
        
        $query = $this->ci->menu_model->getMenuParent($menuId,$status);
        
        if($query == false){
            return null;
        }else{
            $prev = 0;
            $dem = 0;
            foreach($query as $row) {
                $arr = array();
                $menu_parent['id'] = $row->id;
                $menu_parent['name'] = $row->title;
                $menu_parent['parameters'] = $row->parameters;
                $menu_parent['link'] = $row->link;
                $menu_parent['order'] = $row->display_order;
                $menu_parent['status'] = $row->status;
                $menu_parent['previous'] = $prev;
                $menu_parent['next'] = 0;
                $menu_parent['parent'] = $this->menuParent($row->id,$status);
                $menu[$dem] = $menu_parent;
                
                $prev = $row->id;
                //update lai menu truoc do
                if($dem > 0){
                    $menu[$dem-1]['next'] = $row->id;
                }
                $dem++;
            }
        }
        return $menu;
    }
    
    function html_menu_admin($template, $menu){
        $html = '';
        if($menu){
            $html .= $this->load_menu_admin($template,$menu,'','');
        }
        return $html;
    }
    
    function load_menu_admin($template,$menu,$html,$path){
        $str = '';
        
        foreach($menu As $item){
            $str .= $this->ci->load->view($template, array('menu'=>$item,'path'=>$path), true);
            if( $item['parent'] != null){
                $char = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                $str .= $this->load_menu_admin($template,$item['parent'],$html,$path.$char);
            }
        }
        $html .= $str;
        return $html;
    }
    
}